package controleur;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;

import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPConnectionClosedException;
import org.apache.commons.net.ftp.FTPReply;

public class ClientFtp {

	String server;
	String username;
	String password;
	String remote;
	String local;
	ArrayList<File> listFiles;
	final FTPClient ftp;

	public ClientFtp(String server, String username, String password,
			String remote) {
		this.server = server;
		this.username = username;
		this.password = password;
		this.remote = remote;
		ftp = new FTPClient();
		// suppress login details
		ftp.addProtocolCommandListener(new PrintCommandListener(
				new PrintWriter(System.out), true));
	}

	public ClientFtp(String server, String username, String password,
			String remote, String local) {
		this(server, username, password, remote);
		// this.server = server;
		// this.username = username;
		// this.password = password;
		// this.remote = remote;
		this.local = local;

	}

	private void connect() {
		try {
			int reply;
			ftp.connect(server);
			System.out.println("Connected to " + server + " on "
					+ ftp.getDefaultPort());

			// After connection attempt, you should check the reply code to
			// verify
			// success.
			reply = ftp.getReplyCode();

			if (!FTPReply.isPositiveCompletion(reply)) {
				ftp.disconnect();
				System.err.println("FTP server refused connection.");
				System.exit(1);
			}
		} catch (IOException e) {
			if (ftp.isConnected()) {
				try {
					ftp.disconnect();
				} catch (IOException f) {
					// do nothing
				}
			}
			System.err.println("Could not connect to server.");
			e.printStackTrace();
			System.exit(1);
		}
		try {
			if (!ftp.login(username, password))
				ftp.logout();

			System.out.println("Remote system is " + ftp.getSystemType());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ftp.enterLocalPassiveMode();

	}
	
	private void disconnect() {
		if (ftp.isConnected()) {
			try {
				ftp.disconnect();
			} catch (IOException f) {
				// do nothing
			}
		}
	}

	public void retrieveFile() {

		connect();

		try {
			OutputStream output;

			output = new FileOutputStream(local);

			ftp.retrieveFile(remote, output);

			output.close();

			ftp.noop(); // check that control connection is working OK

			ftp.logout();
		} catch (FTPConnectionClosedException e) {
			System.err.println("Server closed connection.");
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			disconnect() ;
		}
	}

//	public void storeFile(String path, File fic) {
//		connect();
//
//		try {
//			ftp.setFileType(FTP.BINARY_FILE_TYPE);
//			InputStream input = null;
//			remote = "/pub/PAM/visucobalt/images/" + fic.getName();
//			try {
//				input = new FileInputStream(fic);
//				ftp.storeFile(remote, input);
//				input.close();
//			} catch (FileNotFoundException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			ftp.noop(); // check that control connection is working OK
//			ftp.logout();
//		} catch (FTPConnectionClosedException e) {
//			System.err.println("Server closed connection.");
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		} finally {
//			if (ftp.isConnected()) {
//				try {
//					ftp.disconnect();
//				} catch (IOException f) {
//					// do nothing
//				}
//			}
//		}
//	}

	public void setListFiles(ArrayList<File> lf) {
		this.listFiles = lf ;
	}
	
	public void storeFiles() {
		connect();
		try {
			ftp.changeWorkingDirectory(remote);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			ftp.setFileType(FTP.BINARY_FILE_TYPE);
			InputStream input = null;

			try {
				for (File file : listFiles) {
					input = new FileInputStream(file);
//					ftp.storeFile(remote + file.getPath().replace('\\', '/'), input);
					ftp.storeFile(file.getPath().replace('\\', '/'), input);
					input.close();
				}
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			ftp.noop(); // check that control connection is working OK
			ftp.logout();
		} catch (FTPConnectionClosedException e) {
			System.err.println("Server closed connection.");
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			disconnect() ;
		}
	}
}
